from django.http import HttpResponse

from apps.api.models import models


def index(request):
    print("many2many")
    # models.Boy.objects.create(name="宝强")
    # models.Boy.objects.create(name="羽凡")
    # models.Boy.objects.create(name="乃亮")
    #
    # models.Girl.objects.bulk_create(
    #     objs=[models.Girl(name="小路"), models.Girl(name="百合"), models.Girl(name="马蓉")],
    #     batch_size=3
    # )

    # 创建关系
    # models.B2G.objects.create(bid_id=1, gid_id=3, address="北京")
    # models.B2G.objects.create(bid_id=1, gid_id=2, address="北京")
    # models.B2G.objects.create(bid_id=2, gid_id=2, address="北京")
    # models.B2G.objects.create(bid_id=2, gid_id=1, address="北京")
    #
    # b_obj = models.Boy.objects.filter(name='宝强').first()
    # g_object = models.Girl.objects.filter(name="小路").first()
    # models.B2G.objects.create(bid=b_obj, gid=g_object, address="北京")

    # 1.宝强都与谁约会。 queyset=[obj,obj,obj]
    v1 = models.B2G.objects.filter(bid__name='宝强').select_related("gid")
    print(v1.query)
    for item in v1:
        print(item.id, item.address, item.bid.name, item.gid.name)

    # 2.百合 都与谁约会。 返回dict
    v2 = models.B2G.objects.filter(gid__name='百合').values("id", 'bid__name', 'gid__name')
    for item in v2:
        print(item['id'], item['bid__name'], item['gid__name'])

    # 3.删除
    models.B2G.objects.filter(bid=1).delete()
    models.Boy.objects.filter(id=1).delete()

    return HttpResponse("many2many index")
